package org.vectrics.mail;


import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import org.vectrics.SystemException;
import org.vectrics.config.MailConfiguration;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.Person;
import org.apache.log4j.Category;


public class UserMailer extends MailSender {
    private static Category log = Category.getInstance(UserMailer.class);
    private MailConfiguration mailConfig = null;
    
    public UserMailer(MailConfiguration mailConfig) {
    	this.mailConfig = mailConfig;
    }

    public void sendMessage(Person person, MailMessage mailMessage) {
    	Collection persons = new HashSet();
    	persons.add(person);
    	sendMessage(persons, mailMessage);
    }

    public void sendMessageAsynch(Person person, MailMessage mailMessage) throws MailException {
    	Collection persons = new HashSet();
    	persons.add(person);
    	sendMessageAsynch(persons, mailMessage);
    }

    public void sendMessageAsynch(Collection persons, MailMessage mailMessage) throws MailException {
//        if ((mailMessage.getSubject() == null) || (mailMessage.getSubject().length() <= 0)) {
//            throw new MailException("E-mail subject was null");
//        }
//        if (CoreServiceLocator.getConfiguration().getSimulateEmailing()) {
//        	placeEmailInfoInLog(persons, mailMessage);
//        } else {
//	        UserMailThread thread = new UserMailThread(this.getClass(), this.mailConfig, persons, mailMessage);
//	        thread.start();
//        }
    }

    public void sendMessage(Collection persons, MailMessage mailMessage) {
//        if ((mailMessage.getSubject() == null) || (mailMessage.getSubject().length() <= 0)) {
//            throw new SystemException("E-mail subject was null");
//        }
//        if (CoreServiceLocator.getConfiguration().getSimulateEmailing()) {
//        	placeEmailInfoInLog(persons, mailMessage);
//        } else {
//	        UserMailThread thread = new UserMailThread(this.getClass(), this.mailConfig, persons, mailMessage);
//	        thread.start();
//        }
    }
    
	protected void placeEmailInfoInLog(Collection persons, MailMessage mailMessage) {
		StringBuffer logMsg = new StringBuffer();
		logMsg.append("Email simulation, e-mail that would be sent:");
		Iterator iter = persons.iterator();
		while (iter.hasNext()) {
			//Long personId = (Long)iter.next();
			//Person person = CoreServiceLocator.getPersonService().findPersonById(personId);
			Person person = (Person)iter.next();
			String email = null;
			if (person.getContactInfo() != null)
				email = person.getContactInfo().getEmail().trim();
	    	logMsg.append("\r\nTo: " + person.getName() + " / " + email);
		}
		logMsg.append("\r\nFrom: " + mailMessage.getFromName() + " / " + mailMessage.getFromAddress());
		logMsg.append("\r\nSubject: " + mailMessage.getSubject());
		logMsg.append("\r\nBody: " + mailMessage.getBody());
		log.info(logMsg.toString());
	}
    
}